#include "get_arg.h"
#include <boost/filesystem/path.hpp>
#include <iostream>
#include <stdlib.h>
#include <sstream>
#include <fstream>
#include <matrix.h>
#include <print_error.h>
#include <matrix_io.h>
#include <portable_timer.h>
#include <boost/lexical_cast.hpp>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <blast_results.h>
#include <pstream.h>
#include <ultoa.h>
#include <errno.h>


using namespace std;
using namespace bcpp_matrix;
using boost::lexical_cast;
using std::ostringstream;
using std::istringstream;
using std::ofstream;
using std::ifstream;

unsigned get_file_size(const string& name)
{
	struct stat file_stat;
	if(stat(name.c_str(), &file_stat))
		return 0;
	return file_stat.st_size;

}


	string get_system_err_str(int errnum)
	{
		char buf [4096];
		if (!strerror_r(errnum, buf, sizeof buf))
			return string();
		else
			return string(buf);
	}


#include <boost/algorithm/string.hpp>
int main (int argc, char *argv[])
{
	try
	{
		string exe_name = "ls /etc";

		redi::rpstream rs(exe_name, redi::pstreams::pstdout | redi::pstreams::pstderr);
		string output_str;
		string line;
		while (getline(rs.err(), line))
			output_str += line + "\n";
		if(boost::find_first(output_str, "command not found"))
			 throw std::runtime_error("Could not find [" + exe_name + "] ");
		rs.clear();
		while (getline(rs.out(), line))
			output_str += line + "\n";
		rs.clear();

		cerr << output_str;
	}
	catch(std::exception& e)
	{
		std_print_error(cerr,  e.what());
	}

}


